package com.isoftstone.bank.controller;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.common.entity.SysRole;
import com.isoftstone.common.entity.SysUser;
import com.isoftstone.common.service.impl.SysMenuService;
import com.isoftstone.common.service.impl.SysRoleMenuService;
import com.isoftstone.common.service.impl.SysRoleService;
import com.isoftstone.common.service.impl.SysUserService;
import com.isoftstone.util.Const;
import com.isoftstone.util.PageData;
import com.isoftstone.util.Tools;
import com.isoftstone.util.UuidUtil;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 文俊 on 2017/12/13.
 */
@Controller
public class RoleController extends BaseController {

    @Autowired
    private SysRoleService service;
    @Autowired
    private SysRoleMenuService menuRoleService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysMenuService menuService;

    @RequiresPermissions("bms-menu.001.002")
    @RequestMapping("/bank/role")
    public ModelAndView toRolePage(){
        return new ModelAndView("/bank/role/role_list");
    }

    @RequestMapping("/bank/role/getRolelist")
    @ResponseBody
    public Object getRolelist(Page page){
        Tidings<List<SysRole>> tidings = new Tidings<>();
        PageData pd = this.getPageData();
        page.setPd(pd);
        List<SysRole> list= service.getBankRolePage(page);
        tidings.setT(list).setPage(page);
        tidings.setStatus(Const.SUCCESS);
        return tidings;
    }

    @RequestMapping("/bank/role/getRoleTree")
    @ResponseBody
    public Object getRoleTree(){
        Tidings<List<SysRole>> tidings = new Tidings<>();
        List<SysRole> list= service.getBankRoleTree();
        tidings.setT(list);
        tidings.setStatus(Const.SUCCESS);
        return tidings;
    }



    @RequestMapping("/bank/role/editQuery")
    @ResponseBody
    public Object editQuery(String roleId){
        Tidings<SysRole> tidings = new Tidings<>();
        if(Tools.notEmpty(roleId)){
            SysRole bean = null;
            try {
                bean = service.getRoleById(roleId);
                List<String> menus = menuRoleService.getMenusByRoleIdOnlyLeaf(roleId);
                String tmp="";
                for (String menuId: menus) {
                    tmp +=menuId + ",";
                }
                if(StringUtils.isNotEmpty(tmp))
                    bean.setMenus(tmp.substring(0,tmp.length()-1));
            } catch (Exception e) {
                e.printStackTrace();
            }
            tidings.setT(bean);
        }
        tidings.setStatus(Const.SUCCESS);
        return tidings;
    }

    @RequestMapping("/bank/role/save")
    @ResponseBody
    public Object save(@ModelAttribute("role") SysRole role){
        Tidings<String> tidings = new Tidings<>();
        try {
            if (StringUtils.isEmpty(role.getRoleId())) {
                role.setRoleId(UuidUtil.getNumFormatUUID());
                role.setRoleCode(role.getRoleId());
                role.setSystem("01");
                role.setStatus("1");
                role.setCorpId(this.getUser().getCorpId());
                role.setCreateDate(new Date());
                role.setCreator(this.getUser().getUserid());
                service.saveRole(role);
                tidings.setMsg("保存成功！");
            } else {
                role.setUpdateDate(new Date());
                role.setUpdator(this.getUser().getUserid());
                service.updateRole(role);

                tidings.setMsg("修改成功！");
            }
            tidings.setStatus(Const.SUCCESS);
            menuRoleService.deleteRoleMenu(role.getRoleId());
            String[] menus = role.getMenus().split(",");
            String[] parents = menuService.getMenusParentIds(menus);
            logger.warn(ArrayUtils.toString(parents));
            menus = (String[])ArrayUtils.addAll(menus,parents);
            for (String menuId:menus) {
                if(StringUtils.isNotEmpty(menuId))
                    menuRoleService.addRoleMenus(role.getRoleId(),menuId);
            }
        } catch (Exception e) {
            tidings.setStatus(Const.ERROR);
            tidings.setMsg("保存失败！");
            e.printStackTrace();
        }
        return tidings;
    }

    @RequestMapping("/bank/role/del")
    @ResponseBody
    public Object del(String roleId){
        Tidings<String> tidings = new Tidings<>();
        try {
            SysUser sysUser=sysUserService.getUserByRoleId(roleId);
            if(StringUtils.equals(sysUser.getCount(),"0")) {
                service.deleteRole(roleId);
                menuRoleService.deleteRoleMenu(roleId);
                tidings.setStatus(Const.SUCCESS);
                tidings.setMsg("删除成功！");
            }else {
                tidings.setStatus(Const.SUCCESS);
                tidings.setMsg("请先删除角色下绑定用户，再进行此操作！");
            }
        }catch (Exception e){
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("删除失败！");
            e.printStackTrace();
        }
        return tidings;
    }

}
